package com.media.publish.utils

import com.baselibrary.UserInfo
import com.baselibrary.activity.BaseActivity
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.utils.SpUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 *@author:hanke
 *2022/8/5
 *desc:
 */
object CommonBusiness {
    fun shareSuccess(activity: BaseActivity, type:Int=2, tip:String) {
        MyHttpUtil.getInstance().getApi(com.baselibrary.net.api.WineCoinsApi::class.java).getWineCoins(SpUtils.getInstance().loadUserBean.data.token, type).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<Int>(this) {
            override fun onSuccess(result: Int?, json: String?) {
                if (result != null) {
                    if (result > 0) {
                        try {
                            activity.showCoinDialog(result,tip)
                            updateUserInfo()
                        } catch (e: Exception) {

                        }
                    }
                }
            }
        })
    }

    fun updateUserInfo() {
        MyHttpUtil.getInstance().getApi(WineCoinsApi::class.java).upUserInfo(SpUtils.getInstance().loadUserBean.data.token).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<UserInfo.DataBean>(this) {
            override fun onSuccess(result: UserInfo.DataBean?, json: String?) {
                if (result != null) {
                    result.token = SpUtils.getInstance().loadUserBean.data.token
                    val mUserInfo = UserInfo()
                    mUserInfo.data = result
                    SpUtils.getInstance().putLoadUserBean(mUserInfo)
                }
            }
        })

    }
}